package com.tbit.uqbike.service.base.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.AccountUserLastPos;
import com.tbit.uqbike.object.pojo.GeoRegionBound;
import com.tbit.uqbike.object.pojo.TerPosition;
import com.tbit.uqbike.service.base.AccountUserLastPosService;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.GeoRegionBoundService;
import com.tbit.uqbike.service.business.RoleService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserLastPosDao;
import com.tbit.uqbike.webmanager.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2021-03-25 17:40
 * 运维人员最后位置
 */
@Service("accountUserLastPosService")
public class AccountUserLastPosServiceImpl implements AccountUserLastPosService {

    @Autowired
    private AccountUserLastPosDao accountUserLastPosDao;
    @Autowired
    private GeoRegionBoundService geoRegionBoundService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AccountUserService accountUserService;

    @Override
    public void insert(AccountUserLastPos accountUserLastPos) {
        accountUserLastPosDao.insert(accountUserLastPos);
    }

    @Override
    public void update(TerPosition terPositions) {
        accountUserLastPosDao.update(terPositions);
    }


    @Override
    public AccountUserLastPos getByAccountUserId(Integer accountUserId) {
        return accountUserLastPosDao.getByAccountUserId(accountUserId);
    }

    @Override
    public List<AccountUserLastPos> getByAccountId(Integer agentId, Integer userType, Integer usermainId, List<Integer> regionId,Integer accountUserId,Integer pageNO,Integer rowCount) {

        /**查询代理商运维账号*/
        List<Integer> accountUserIds = new ArrayList<Integer>();
        /**判断当前账号是不是运维人员*/
        if (userType.equals(3) && Objects.isNull(accountUserId)) {
            accountUserIds.add(usermainId);
        }

        if (Objects.nonNull(accountUserId)){
            accountUserIds.add(accountUserId);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", agentId);
        params.put("userMainIds", StringUtil.getListString(accountUserIds));
        params.put("offset",Objects.nonNull(pageNO) && Objects.nonNull(rowCount) ? ((pageNO - 1) * rowCount) : null);
        params.put("rowCount",rowCount);
        if (CollUtil.isNotEmpty(regionId)) {
            List<GeoRegionBound> georeGionBounds = geoRegionBoundService.queryByIds(regionId);
            accountUserIds = georeGionBounds.stream().map(GeoRegionBound::getAccountUserId)
                    .filter(Objects::nonNull).collect(Collectors.toList());
            if (CollUtil.isEmpty(accountUserIds)) {
                return new LinkedList<>();
            }
            params.put("userMainIds", StringUtil.getListString(accountUserIds));
        }

        List<AccountUserLastPos> accountUserLastPos = accountUserLastPosDao.getByAccountId(params);
        /*if (CollectionUtils.isNotEmpty(accountUserLastPos)) {
            Set<Integer> accountUserIdSet = accountUserLastPos.stream().map(AccountUserLastPos::getAccountUserId).filter(Objects::nonNull).collect(Collectors.toSet());
            if (CollectionUtils.isEmpty(accountUserIdSet)){
                return accountUserLastPos;
            }
            List<AccountUser> accountUserList = accountUserService.getAccountUserByIds(accountUserIdSet);
            if (CollectionUtils.isEmpty(accountUserList)){
                return accountUserLastPos;
            }
            Map<Integer, AccountUser> accountUserMap = accountUserList.stream().collect(Collectors.toMap(AccountUser::getAccountUserId, Function.identity()));
            for (AccountUserLastPos dls : accountUserLastPos) {
                AccountUser accountUser = accountUserMap.get(dls.getAccountUserId());
                if (Objects.nonNull(accountUser)) {
                    dls.setName(accountUser.getName());
                }
            }
        }*/
        return accountUserLastPos;
    }

    @Override
    public List<AccountUserLastPos> getAll() {
        return accountUserLastPosDao.getAll();
    }
}
